<?php

namespace auxiliary\traits;


use auxiliary\timer\TimerGroup;
use handle\message\lib\driver\EventDriver;
use handle\message\lib\Entrance;
use handle\message\lib\factory\SingletonFactory;
use handle\message\lib\manage\ChessBoardManage;
use handle\message\lib\manage\PlayerManage;
use handle\message\lib\manage\RoomManage;
use handle\message\lib\manage\strategy\ChessBoard;
use handle\message\log\CustomException;

trait Singleton
{
    /**
     * 单例
     * @var
     */
    private static $instance;

    /**
     * 获取实例
     * @return ChessBoard|ChessBoardManage|EventDriver|Entrance|SingletonFactory|RoomManage|PlayerManage|CustomException|TimerGroup
     */
    public static function getInstance()
    {
        if(!isset(self::$instance)){
            self::$instance = new self();
            self::$instance->beforeOnlyUseOnce();
        }
        self::$instance->initialization();
        self::$instance->after();
        return self::$instance;
    }

    /**
     * 初始化信息
     * @return void
     */
    protected function initialization(){}

    /**
     * 在初始化之前只使用一次
     * @return void
     */
    protected function beforeOnlyUseOnce(){}

    /**
     * 在获取到后调用
     * @return void
     */
    protected function after(){}

    /**
     * 私有构造函数，防止实例被new所创建
     */
    private function __construct(){}

    /**
     * 私有克隆函数，防止被克隆
     * @return void
     */
    private function __clone(){}
}